import request from '@/utils/request'


import { Dialog } from 'vant';


//下载和预览
export function commonFileInterface(data) {
  return request({
    url: '/innovate/commonFileInterface',
    method: 'post',
    data,
    responseType: 'blob',
  })
}

export function commonFileInterfacepath(data) {
  return request({
    url: '/innovate/commonFileInterfacepath',
    method: 'post',
    data,
    
  })
}

export async function pdfdown(item) {
  var data = await commonFileInterface({
    tableName: item.tableName,
    sysId: item.sysId,
    fileName: item.fileName
  })
  if (data.data) {

    // if (data.data.size == 0) {
    //   MessageBox.alert('文件不存在，请联系管理员');
    //   return
    // }

    let result = data.data
    let _fileName = data.headers['content-disposition'].split(';')[1].split('=')[1]; //拆解获取文件名，
    //console.log(_fileName)
    let link = document.createElement('a')
    link.href = URL.createObjectURL(
      new Blob([result], {type: 'application/pdf'})
    )
    link.setAttribute('download', _fileName);
    document.body.appendChild(link)
    link.click()
    window.URL.revokeObjectURL(url); // 释放内存

  }
}

export  function pdfdown1(item) {
  //console.log("request",util.serverhost+ 'innovate/commonFileInterface2?tableName='+item.tableName+'&sysId='+item.sysId+'&fileName='+item.fileName)


var url='http://218.89.178.103:9000/innovate/commonFileInterface2?tableName='+item.tableName+'&sysId='+item.sysId+'&fileName='+item.fileName;
 
window.location.href=url
}

export async function onlineread(item) {
  var data1 = await commonFileInterface({
    tableName: item.tableName,
    sysId: item.sysId,
    fileName: item.fileName
  })
  if (data1.data) {
   // const blob = new Blob([data1.data],{type:'application/pdf'})
    if (data1.data.data.size==0) {
     
      Dialog.alert({
        message: '文件不存在，请联系管理员',
      }).then(() => {
        // on close
      });
      return
    }
    else{
      
    let result = data1.data.data

    const url = window.URL.createObjectURL(new Blob([result], {type: "application/pdf"}))

      return  url

  }
}
}
